package com.gxuwz.medical.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.institution.Institution;

/**
 * 农合经办机构管理控制处理模块
 * 
 * @author 演示
 * 
 */
public class InstitutionServlet extends BaseServlet {

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String m = request.getParameter("m");// 动作类型参数
		if ("list".equals(m)) {
			process(request, response, "/page/institution/institution_list.jsp");
		} else if ("input".equals(m)) {
			process(request, response, "/page/institution/institution_add.jsp");
		} else if ("get".equals(m)) {
			process(request, response, "/page/institution/institution_edit.jsp");
		} else if ("add".equals(m)) {
			add(request, response);
			process(request, response, "/page/institution/institution_list.jsp");
		} else if ("edit".equals(m)) {
			edit(request, response);
			process(request, response, "/page/institution/institution_list.jsp");
		} else if ("del".equals(m)) {
			del(request, response);
			process(request, response, "/page/institution/institution_list.jsp");
		} else {
			error(request, response);
		}
	}

	/**
	 * 添加机构方法
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 接收页面传递过来的参数
		String areacode = request.getParameter("areacode");
		String agencode = request.getParameter("agencode");
		String agenname = request.getParameter("agenname");// 有可能出现中文乱码
		// 实例化Institution类
		Institution institution = new Institution();
		int grade = areacode.length() == 6 ? 1 : 2;
		// 调用添加方法
		try {
			Area area = new Area(areacode);
			institution.addInst(area, agencode, agenname, grade);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 自己写的修改删除
	 */

	private void edit(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		// 接收行政区域编码+行政区域名称，行政区域名称有可能出现中文乱码
		String areacode = req.getParameter("areacode");
		String agencode = req.getParameter("agencode");
		String agenname = req.getParameter("agenname");
		// 实例化Institution类
		int grade = areacode.length() == 6 ? 1 : 2;
		try {
			Area area = new Area(areacode);
			Institution institution = new Institution(area);
			institution.editInst(agenname, agencode);
		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	private void del(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String areacode = req.getParameter("id");

		try {
			Area area = new Area(areacode);
			Institution institution = new Institution(area);
			institution.delInst(area);
		} catch (Exception e) {
			e.printStackTrace();

		}
	}
}
